package com.link2room.ironhide.facade.impl;

import com.link2room.common.constant.BaseConstant;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.PinyinUtil;
import com.link2room.common.util.TryExceptionUtil;
import com.link2room.ironhide.dto.param.api.GroupAreaApiParamDto;
import com.link2room.ironhide.dto.result.api.L2RGroupAreaApiDto;
import com.link2room.ironhide.dto.result.api.L2RGroupCityApiDto;
import com.link2room.ironhide.dto.result.api.L2RGroupCityDetailApiDto;
import com.link2room.ironhide.dto.result.base.L2RGroupAreaDto;
import com.link2room.ironhide.dto.result.base.PositionBaseDto;
import com.link2room.ironhide.entity.L2RGroupArea;
import com.link2room.ironhide.entity.L2RGroupPositionBase;
import com.link2room.ironhide.facade.IGroupAreaApiFacadeService;
import com.link2room.ironhide.service.IL2RGroupPositionBaseService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by sss on 2016/10/11.
 */
@Component("groupAreaApiFacadeService")
public class GroupAreaApiFacadeServiceImpl implements IGroupAreaApiFacadeService {
    @Resource
    private ICommonService commonService;
    @Resource
    private IL2RGroupPositionBaseService l2RGroupPositionBaseService;

    /**
     * 获取集团城市列表
     * @param groupAreaApiParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RGroupAreaApiDto listL2RGroupCity(GroupAreaApiParamDto groupAreaApiParamDto)throws Exception{
        L2RGroupArea querylga=new L2RGroupArea();
        querylga.setLhotelGroupCode(groupAreaApiParamDto.getLhotelGroupCode());
        querylga.setIsHalt(DefaultValue.F);
        HashMap<String,String> orderBy=new HashMap<>();
        orderBy.put(L2RGroupArea.L2RGroupArea_.citySearchCode.toString(), CriteriaUtil.DESC);
        QueryResult<L2RGroupArea> l2rGroupAreaResult = commonService.findAllEQ(L2RGroupArea.class, querylga, null, null, orderBy, null);
        List<L2RGroupArea> l2rGroupAreaList = l2rGroupAreaResult.getResultlist();
        L2RGroupAreaApiDto l2RGroupAreaApiDto=new L2RGroupAreaApiDto();
        if(l2rGroupAreaList!=null&&l2rGroupAreaList.size()>0){
            Map<String,L2RGroupCityApiDto> letterMap=new LinkedHashMap<>();
            int letterSize=0;
            List<Long> cityCodeList = l2rGroupAreaList.stream().map(event -> event.getCityCode()).collect(Collectors.toList());
            L2RCondition l2RCondition=new L2RCondition();
            l2RCondition.setAndConditions(new ConditionDetail[]{
                    new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,groupAreaApiParamDto.getLhotelGroupCode()),
                    new ConditionDetail(L2RGroupPositionBase.L2RPositionBase_.positionType.toString(), CriteriaUtil.Operator.EQ,"District"),
                    new ConditionDetail(L2RGroupPositionBase.L2RPositionBase_.cityCode.toString(), CriteriaUtil.Operator.IN,cityCodeList),
                    new ConditionDetail(L2RGroupPositionBase.L2RPositionBase_.name.toString(), CriteriaUtil.Operator.LIKE,"%市"),
            });
            QueryResult<L2RGroupPositionBase> l2RGroupPositionBaseQueryResult = commonService.findAllByCondition(L2RGroupPositionBase.class, l2RCondition);
            List<L2RGroupAreaDto> l2RGroupAreaDtos = Optional.ofNullable(l2RGroupPositionBaseQueryResult.getResultlist()).map(list -> list.stream().map(TryExceptionUtil.of(
                    event -> {
                        L2RGroupAreaDto l2RGroupAreaDto = new L2RGroupAreaDto();
                        l2RGroupAreaDto.setCityName(event.getName());
                        l2RGroupAreaDto.setCitySearchCode(PinyinUtil.getStringPinYin(event.getName()));
                        l2RGroupAreaDto.setCityCode(Long.parseLong(event.getPositionCode()));
                        l2RGroupAreaDto.setIsCity(DefaultValue.F);
                        return l2RGroupAreaDto;
                    }
            )).collect(Collectors.toList())).orElse(Collections.emptyList());
            List<L2RGroupAreaDto> baselist=ClassConverUtil.copyPropertiesListToDtoList(l2rGroupAreaList,L2RGroupAreaDto.class,false);
            baselist.addAll(l2RGroupAreaDtos);
            for(String letter:BaseConstant.LettersConstant.Letters){
                boolean flag=false;
                L2RGroupCityApiDto l2RGroupCityApiDto=new L2RGroupCityApiDto();
                List<L2RGroupCityDetailApiDto> citys=new ArrayList<>();
                L2RGroupCityDetailApiDto l2RGroupCityDetailApiDto=null;
                int size=0;
                for(L2RGroupAreaDto l2rGroupArea:baselist){
                    if(l2rGroupArea.getCitySearchCode().startsWith(letter)||l2rGroupArea.getCitySearchCode().startsWith(letter.toLowerCase())){
                        flag=true;
                        l2RGroupCityDetailApiDto=new L2RGroupCityDetailApiDto();
                        l2RGroupCityDetailApiDto.setCityName(l2rGroupArea.getCityName());
                        l2RGroupCityDetailApiDto.setCityCode(l2rGroupArea.getCityCode());
                        l2RGroupCityDetailApiDto.setCitySearchCode(l2rGroupArea.getCitySearchCode());
                        l2RGroupCityDetailApiDto.setHotelNum(l2rGroupArea.getHotelNum());
                        l2RGroupCityDetailApiDto.setIsCity(l2rGroupArea.getIsCity());
                        citys.add(l2RGroupCityDetailApiDto);
                        size++;
                    }
                }
                if(flag){
                    l2RGroupCityApiDto.setCitys(citys);
                    l2RGroupCityApiDto.setSize(size);
                    letterMap.put(letter,l2RGroupCityApiDto);
                    letterSize++;
                }
            }
            l2RGroupAreaApiDto.setLetter(letterMap);
            l2RGroupAreaApiDto.setSize(letterSize);
        }
        return l2RGroupAreaApiDto;
    }

    /**
     * 获取集团热门城市列表
     * @param groupAreaApiParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<L2RGroupCityDetailApiDto> listL2RGroupHotCity(GroupAreaApiParamDto groupAreaApiParamDto)throws Exception{
        L2RGroupArea querylga=new L2RGroupArea();
        querylga.setLhotelGroupCode(groupAreaApiParamDto.getLhotelGroupCode());
        querylga.setIsHot("T");
        HashMap<String,String> orderBy=new HashMap<>();
        orderBy.put(L2RGroupArea.L2RGroupArea_.citySearchCode.toString(), CriteriaUtil.DESC);
        QueryResult<L2RGroupArea> l2rGroupAreaResult = commonService.findAllEQ(L2RGroupArea.class, querylga, null, null, orderBy, null);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rGroupAreaResult,L2RGroupCityDetailApiDto.class, false);
    }

    /**
     * 获取集团区域列表
     * @param groupAreaApiParamDto
     * @return
     * @throws Exception
     */
    @Override
    public List<PositionBaseDto> listGroupPositionBase(GroupAreaApiParamDto groupAreaApiParamDto)throws Exception{
        List<PositionBaseDto> positionBaseList = l2RGroupPositionBaseService.listGroupPositionBase(groupAreaApiParamDto.getLhotelGroupCode(), groupAreaApiParamDto.getCityCode());
        return positionBaseList;
    }
}
